package com.fujimana.android.manacode;

import junit.framework.Assert;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class TreeView extends ListView {

	public void setOnTreeItemClickListener(
			TreeView.OnTreeItemClickListener listener) {
		_lisner = listener;

	}

	private OnTreeItemClickListener _lisner;

	interface OnTreeItemClickListener {
		void onItemClick(AdapterView<?> parent, View view, Object target);
	}

	private TreeAdapter<?> _treeAdapter;

	public TreeView(Context context) {
		this(context, null);

	}

	public TreeView(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.setDividerHeight(0);

		super.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if (_treeAdapter != null) {
					Object object = _treeAdapter.getItem(position);
					Assert.assertNotNull(object);

					_lisner.onItemClick(parent, view, object);
				}

			}

		});
	}

	public void setTreeAdapter(TreeAdapter<?> adapter) {
		_treeAdapter = adapter;
		super.setAdapter(_treeAdapter);

	}

}
